package com.leyou.item.controller;

import com.leyou.common.utils.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.Impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: Saka
 * Date: 2019/1/20
 * Time: 18:59
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 商品列表查询
     *
     * @param key
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "valid",required = false)Boolean valid

    ) {

        PageResult<SpuBo> result = this.goodsService.querySpuByPageAndSort(key, page, rows, saleable,valid);

        if (result != null && null != result.getItems() && 0 != result.getItems().size()) {
            return ResponseEntity.ok(result);

        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();//201
    }

    /**
     * 查询spuDetail接口
     *
     * @param id
     * @return
     */
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id") Long id) {
        SpuDetail spuDetail = this.goodsService.querySpuDetailById(id);

        if (spuDetail != null) {
            return ResponseEntity.ok(spuDetail);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long id) {
        List<Sku> skuList = this.goodsService.querySkuBySpuId(id);
        if (skuList != null && 0 != skuList.size()) {
            return ResponseEntity.ok(skuList);

        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    /**
     * 修改商品列表中的商品信息
     *
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
        this.goodsService.updateGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();//201
    }

    /**
     * 下架商品
     *
     * @param id
     * @return
     */
    @PutMapping("downShelves/{id}")
    public ResponseEntity<Void> downShelves(
            @PathVariable("id") Long id

    ) {
        this.goodsService.downShelves(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *
     * 上架商品
     * @param id
     * @return
     */
    @PutMapping("onShelves/{id}")
    public ResponseEntity<Void> onShelves(
            @PathVariable("id") Long id

    ) {
        this.goodsService.onShelves(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除商品(逻辑删除)
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteShelves/{id}")
    public ResponseEntity<Void> deleteShelves(
            @PathVariable("id") Long id

    ) {
        this.goodsService.deleteShelves(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
